'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview  联盟任务系统
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionMissionConf = require('../../../../config/unionMissionConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var roleService = require('../../../../services/roleService');
var constantUtil = require('../../../../util/constantUtil.js');

/**
 * 刷新联盟任务
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_task_list = [];
    var free_num = 0;
    var diamond_refresh_num = 0;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var unionTask_model = player_manager.unionTask;

                // 是否还有免费次数
                if (!!unionTask_model.refresh_num) {
                    var refresh_num = unionTask_model.refresh_num;

                    // 刷新当前任务列表
                    unionTask_model.taskRefresh();
                    union_task_list = unionTask_model.unionTask_list;

                    // 扣除免费次数
                    unionTask_model.subFreeRefreshNum();
                    free_num = unionTask_model.refresh_num;
                } else {
                    var now_refresh_num = unionTask_model.now_refresh_num;
                    var sub_diamond = unionMissionConf.refreshDiamond(now_refresh_num).price;

                    // 扣除钻石
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_UNION_TASK_RESF, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }
                    // 刷新当前任务列表
                    unionTask_model.taskRefresh();
                    union_task_list = unionTask_model.unionTask_list;

                    // 增加刷新次数记录
                    unionTask_model.addDiamondRefreshNum();
                    diamond_refresh_num = unionTask_model.now_refresh_num;
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    // union_task_list: union_task_list
                }
            });
        }
    );
};
